import config from "./config";
import CallApp from "callapp-lib";
import { getBrowser } from "./utils";

let CALL = {};

for (const item in config) {
  let name = item.toLocaleUpperCase();

  config[item].fallback = getBrowser().isIos
    ? config[item].appstore
    : config[item].yingyongbao;

  CALL[`Call${name}`] = (path = "", param = null, callback = null) => {
    const options = {
      path: ""
    };
    if (path) options.path = path;
    if (param) options.param = param;
    if (callback) options.callback = callback;

    return new CallApp(config[item]).open(options);
  };
}

const install = (Vue, opts = {}) => {
  if (!Vue || install.installed) {
    return;
  }
  Vue.prototype.$callapp = CALL;
};

export default {
  install,
  ...CALL
};
